package org.anyin.gitee.cloud.center.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.anyin.gitee.cloud.center.base.convert.SysRouteConvert;
import org.anyin.gitee.cloud.center.base.enums.BaseExCodeEnum;
import org.anyin.gitee.cloud.center.base.model.SysRouteModel;
import org.anyin.gitee.cloud.center.base.repository.SysRouteRepository;
import org.anyin.gitee.cloud.center.base.service.SysRouteService;
import org.anyin.gitee.cloud.center.base.service.dto.SysRouteDTO;
import org.anyin.gitee.cloud.center.base.service.dto.SysRoutePageQueryDTO;
import org.anyin.gitee.cloud.center.common.constants.GatewayConstants;
import org.anyin.gitee.cloud.lib.core.cache.CacheTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysRouteServiceImpl implements SysRouteService {

    @Autowired
    private CacheTemplate cacheTemplate;
    @Autowired
    private SysRouteRepository sysRouteRepository;
    @Autowired
    private SysRouteConvert sysRouteConvert;

    @Override
    public void create(SysRouteDTO route) {
        this.checkPathAndRouteName(route.getPath(), route.getRouteName(), null);
        SysRouteModel model = sysRouteConvert.getSysRouteModel(route);
        sysRouteRepository.save(model);
        this.refreshToCache();
    }

    @Override
    public void modify(Long id, SysRouteDTO route) {
        this.checkPathAndRouteName(route.getPath(), route.getRouteName(), id);
        SysRouteModel model = sysRouteConvert.getSysRouteModel(route);
        model.setId(id);
        sysRouteRepository.updateById(model);
        this.refreshToCache();
    }

    @Override
    public SysRouteDTO info(Long id) {
        SysRouteModel model = sysRouteRepository.getById(id);
        return sysRouteConvert.getSysRouteDTOByModel(model);
    }

    @Override
    public void del(Long id) {
        sysRouteRepository.removeById(id);
        this.refreshToCache();
    }

    @Override
    public Page<SysRouteDTO> page(SysRoutePageQueryDTO query) {
        Page<SysRouteModel> page = new Page<>(query.getPageNo(), query.getPageSize());
        page = sysRouteRepository.page(page, query.getPath(), query.getRouteName());
        return sysRouteConvert.getSysRouteDTOPage(page);
    }

    @Override
    public void refreshToCache() {
        List<SysRouteModel> list = sysRouteRepository.all();
        List<SysRouteDTO> routes = sysRouteConvert.getSysRouteDTOList(list);
        cacheTemplate.valueSet(GatewayConstants.SYS_ROUTE_KEY, routes);
    }

    @Override
    public void checkPathAndRouteName(String path, String routeName, Long id) {
        boolean existPath = sysRouteRepository.existPath(path, id);
        if(existPath){
            throw BaseExCodeEnum.SYS_ROUTE_PATH_EXIST.getException();
        }
        boolean existRouteName = sysRouteRepository.existRouteName(routeName, id);
        if(existRouteName){
            throw BaseExCodeEnum.SYS_ROUTE_ROUTE_NAME.getException();
        }
    }
}
